# coding: utf-8

from django.db import models
from apps.core.models import TimeStamped, Addtimed


class DevType(models.Model):
    """设备类型表"""
    USERDEF = (
        (0, u'内置'),
        (1, u'自定义')
    )

    name = models.CharField(max_length=50, verbose_name=u'名称', default='')
    identify = models.CharField(max_length=50, verbose_name=u'编号', default='')
    # T3-1.1 T3-2.0 T3-3.0 T3-3.1 A4 white-net white-stand next_filrewall fortress_machine T9
    ename = models.CharField(max_length=50, verbose_name='别名', default='')
    useradd = models.IntegerField(choices=USERDEF, default=0)
    desc = models.CharField(max_length=100, verbose_name=u'描述', default='')

    class Meta:
        db_table = 't_devtype'

    @staticmethod
    def dev_group(num):
        # 1代表1.1防火墙和网闸，2代表审计--A4,3代表2.0防火墙,4代表下一代防火墙--next_filrewall ,5代表堡垒机--fortress_machine,6代表白名单--white-net 7--white-stand 数组下标和数据库id对应,默认显示为防火墙
        group = [0, 1, 2, 3, 1, 4, 5, 6]
        try:
            return group[num]
        except:
            return 1


class Device(models.Model):
    """
    设备表
    """
    TTYPE = (
        (1, u'工业控制入侵检测系统'),
        (2, u'工业控制防火墙'),
        (3, u'工业控制网络隔离系统'),
        (4, u'工业控制安全审计系统'),
        (5, u'工业网闸'),
        (6, u'工业安全审计平台'),
        (7, u'安全管理平台'),
        (8, u'工业安全防火墙1.1')
    )
    ONLINE = (
        (0, u'离线'),
        (1, u'在线')
    )
    AUTH = (
        (0, u'否'),
        (1, u'是')
    )

    IMPORT = (
        (0, u'一般'),
        (1, u'重要'),
        (2, u'非常重要'),
    )
    name = models.CharField(max_length=100, verbose_name=u'名称', default='')
    dtype = models.ForeignKey(DevType, default=None, on_delete=models.CASCADE)
    importance = models.IntegerField(choices=IMPORT, default=0)
    location = models.CharField(max_length=100, verbose_name=u'所处位置', default='')
    desc = models.CharField(max_length=100, verbose_name=u'描述', default='')
    version = models.CharField(max_length=100, verbose_name=u'软件版本', default='')
    safeowner = models.CharField(max_length=100, verbose_name=u'安全责任人', default='')
    mac = models.CharField(max_length=30, verbose_name=u'mac地址', default='')
    ip = models.CharField(max_length=20, verbose_name=u'ip地址', default='')
    online = models.IntegerField(verbose_name=u'是否在线', choices=ONLINE, default=0)
    endtime = models.DateField()
    isauthenticate = models.IntegerField(verbose_name=u'是否需要鉴权', choices=AUTH, default=0)
    username = models.CharField(max_length=50, verbose_name=u'鉴权用户名', default='')
    passwd = models.CharField(max_length=100, verbose_name=u'鉴权密码', default='')
    devnumber = models.CharField(max_length=100, verbose_name=u'产品型号', default='')
    hardnumber = models.CharField(max_length=100, verbose_name=u'硬件型号', default='')
    assert_no = models.CharField(max_length=100, verbose_name=u'序列号', default='')
    vendor = models.CharField(max_length=255, verbose_name=u'厂商', default='')
    model = models.CharField(max_length=255, verbose_name=u'硬件版本', default='')

    class Meta:
        db_table = 't_device'
        ordering = ["-id"]


class DevEname(models.Model):
    """设备标记型号映射表"""
    IS_SHOW = (
        (0, u'不显示'),
        (1, u'显示')
    )
    name = models.CharField(max_length=50, verbose_name=u'名称', default='')
    ename = models.CharField(max_length=50, verbose_name='别名', default='')
    is_show = models.IntegerField(choices=IS_SHOW, default=1)

    class Meta:
        db_table = 't_device_ename'


class DeviceSnmpConfig(models.Model):
    """
    设备snmp配置表
    """

    device_id = models.IntegerField()
    protoctl = models.CharField(max_length=8, verbose_name=u'协议', default='')
    flow_oid = models.CharField(max_length=1024, verbose_name=u'流量iod', default='')
    disk_oid = models.CharField(max_length=1024, verbose_name=u'磁盘iod', default='')
    version = models.CharField(max_length=6, verbose_name=u'版本号', default='')
    community = models.CharField(max_length=1024, verbose_name=u'团体号', default='')

    class Meta:
        db_table = 't_device_snmp_config'
        ordering = ["-id"]


class DevTaskJob(models.Model):
    """资产探测结果表"""
    STATUS = (
        (0, u'未添加'),
        (1, u'已添加')
    )

    ip = models.CharField(max_length=255, verbose_name=u'资产IP', default='')
    mac = models.CharField(max_length=255, verbose_name='资产MAC', default='')
    vendor = models.CharField(max_length=255, verbose_name='厂商', default='')  # vendor
    model = models.CharField(max_length=255, verbose_name='版本', default='')  # model
    os_type = models.CharField(max_length=255, verbose_name='型号', default='')  # osType
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        db_table = 'asset_task_job'
		

class BatchOperation(models.Model):
    """
    批量设备记录
    """
    STATUS_CHOICES = ((None, u'未启动'), ('-1', u'失败'), ('0', u'执行中'), ('1', u'成功'))
    BEHAVIOR_CHOICES = ((1, u'升级系统'), (2, u'升级特征库'), (3, u'重启设备'))

    name = models.CharField(max_length=255, verbose_name='设备名称', default='')
    ip = models.CharField(max_length=15, verbose_name='设备IP', default='')
    type = models.CharField(max_length=255, verbose_name='设备类型', default='')
    status = models.CharField(max_length=2, choices=STATUS_CHOICES, verbose_name='状态', null=True, blank=True, default=None)
    behavior = models.SmallIntegerField(choices=BEHAVIOR_CHOICES, verbose_name='行为', default=1)
    cause = models.CharField(max_length=255, verbose_name='失败原因', default=None)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'batch_operation'
        ordering = ["-id"]